package com.yhy.wxzn.wcs.utils;

import com.yhy.wxzn.common.enums.YnEnums;
import com.yhy.wxzn.common.exception.WmsBusinessException;
import com.yhy.wxzn.common.utils.ObjectFilterHelper;
import com.yhy.wxzn.wcs.device.rgv.bean.Coord;
import com.yhy.wxzn.wcs.device.rgv.enums.NodeStatus;
import com.yhy.wxzn.wcs.entity.WcsMapInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelCollectionParams;
import org.jeecgframework.poi.excel.entity.params.ExcelImportEntity;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelImportEnum;
import org.jeecgframework.poi.util.ExcelUtil;

import java.io.InputStream;
import java.util.*;

/**
 * @Auther: 97354
 * @Date: 2023/12/9 11:59
 * @Description: MapImportUtils
 * @Version 1.0.0
 */
public class MapImportUtils {

    public static List<WcsMapInfo> importExcel(InputStream inputstream) throws Exception {
        List<WcsMapInfo> result = new ArrayList<>();
        Workbook book = WorkbookFactory.create(inputstream);
        int sheetNum = book.getNumberOfSheets();
        for (int i = 0; i < sheetNum; i++) {
            Sheet sheet = book.getSheetAt(i);
            List<WcsMapInfo> mapInfos = genMapSheet(sheet,i);
            result.addAll(mapInfos);
        }
        return result;
    }

    private static List<WcsMapInfo> genMapSheet(Sheet sheet,int layer) {
        List<WcsMapInfo> result = new ArrayList<>();
        for (int i = 2; i < sheet.getLastRowNum() + 1 ; i++) {
            Row row = sheet.getRow(i);
            for (int j = 2; j < row.getLastCellNum(); j++) {
                String excelValue = row.getCell(j).getStringCellValue().trim();
                WcsMapInfo mapInfo = new WcsMapInfo();


                mapInfo.setNodeStatus(NodeStatus.N.toString());
                mapInfo.setBeDown(excelValue.contains("下"));
                mapInfo.setBeLeft(excelValue.contains("左"));
                mapInfo.setBeRight(excelValue.contains("右"));
                mapInfo.setBeUp(excelValue.contains("上"));



                mapInfo.setX(i-1);
                mapInfo.setY(j-1);
                mapInfo.setZ(layer+1);
                mapInfo.setNodeType(getType(excelValue,mapInfo.getX(),mapInfo.getY(),mapInfo.getZ()));
                mapInfo.setStatus(Boolean.TRUE);
                String coord = String.format("%04d", mapInfo.getX()) +
                        String.format("%04d", mapInfo.getY()) + String.format("%02d", mapInfo.getZ());
                mapInfo.setCoord(coord);
                ObjectFilterHelper.getObjDefault(mapInfo);
                result.add(mapInfo);
            }
        }
        return result;
    }

    private static final String[] type = new String[]{"B","输","货","道","提","充","接"};
    private static Integer getType(String excelValue, Integer x, Integer y, Integer z) {
        for (int i = 0; i < type.length; i++)
            if (excelValue.contains(type[i])) return i;

        throw new RuntimeException(String.format("坐标：%s，单元格数据：%s，无类型值或类型不存在",new Coord(x,y,z).toString(),excelValue));

    }
}
